import { commonPost } from '@api/data'
import { Message, MessageBox, Loading } from "element-ui"

/** 调用医保dll**/
export class CallMedIns{
    constructor() {
        this.name = 'NeusoftInterface';
        this.medinstrade = null;
    }
    async InitAsync() {
        if(typeof(CefSharp) == 'undefined') {
            // eslint-disable-next-line no-console
            console.error("CefSharp is undefined.")
            throw "CefSharp is undefined.";
        } else {
            if(typeof(NeusoftInterface) == 'undefined') {
                await CefSharp.BindObjectAsync(this.name);
            }
        }
        this.medinstrade = NeusoftInterface;
    }
    /**
     * 医保初始化
     *   @param {number} type  1市医保，2省医保
     */
    async Init(type) {
        await this.InitAsync();
        var ret =await this.medinstrade.init(type);
        console.log(`医保初始化返回：`+ret);
        if (!ret) {
            // eslint-disable-next-line no-console
            console.error(`医保初始化失败`);
            throw `医保初始化失败`;
        }
       
    }
       /**
     * 签到
     * * @param {number} type  1市医保，2省医保
     *  * @param {string} req  签到报文
     */
    async Sign(type,req) {
        if(this.medinstrade == null) {
            await this.InitAsync();
        }
        // let ret="1596-01596202-2020071618403309582";
        // return ret;
        let ret =await this.medinstrade.businessHandle(type,req);
        console.log(`医保签到返回：`+ret);
        if(ret== '') {
            throw '医保签到交易失败';
        }
        else{
            let TradeData =  JSON.parse(ret);
            if(TradeData.code != "0"){
                throw('医保签到交易失败：'+TradeData.data);
            }
            else{
                let temp=TradeData.data;
                return  temp;//temp.split("^")[1] ;
            }
        }
    }
     /**
     * 交易
     * * @param {number} type  1市医保，2省医保
     *  * @param {string} req  交易报文
     */
    async Trade(type,req) {
        if(this.medinstrade == null) {
            await this.InitAsync();
        }
        console.log("医保send："+req);
        if(req==null){
            throw '医保发送报文为空，交易终止；请重试。';
        }
        // let ret="0104-20200817143129-774^1^1200095080|02407630200076|220104197111080917|杨云富|1|11|1971-11-08|N00580217|12|||1||222401|313233|2020|0|5217.77|2605.63|1556.56|0.0|0.0|0.0|788.07|0.0|788.07|0.0|0.0|0.0|0.0|0.0|0.0|0|延吉市医院|||2268.63|0.0|2216.44|0.0|0|^";
        // return ret;

        let ret =await this.medinstrade.businessHandle(type,req);
        if(ret== '') {
            throw '医保交易失败';
        }
        else{
            let TradeData =  JSON.parse(ret);
            if(TradeData.code != "0"){
                MessageBox.alert(TradeData.data);
                LogError(`医保Trade失败返回：`+ret);
                throw('医保交易失败：'+TradeData.data);
            }
            else{
                console.log("医保ret："+TradeData.data);
                return TradeData.data;
            }
        }
    }
}
function LogError(str) {
    console.log(`%c ${str}`, 'color:red');
}